<?php 
namespace common\service;
use common\model\sell66\SteamItem;
use common\model\sell66\PriceRule;
use common\model\sell66\GameTagCategory;
use common\model\sell66\SteamItemRatioConfig;
use think\cache;
/**
 * 获取用户库存
 */
class ItemPriceService{

	public static $instance = null;
	
	private function __construct(){
		
	}

	public static function getInstance(){
		if(self::$instance == null){
			self::$instance = new self();
		}
		return self::$instance;
	}
	
	public function calculateItemPrice($item,$appId =730){
		$priceItem = SteamItem::getItemInfoByHashName($appId,$item['market_hash_name']);
		if(empty($priceItem)){
			//我们数据库不存在这类商品的报价  不收
			$item['recovery'] = true;
			$item['price'] = 0;
			$item['originPrice'] = 0;
			$item['item_second'] = 0;
			return $item;

		}
		$item['originPrice'] = $priceItem['price'];
		if($priceItem['app_id'] == 730){
			$priceItem = $this->getSpecialRulePrice($priceItem,$item['desc']);
			$lastPrice = $this->getRecoveryPrice($priceItem,$appId);
		}elseif($priceItem['app_id'] == 570){
			$priceItem = $this->getSpecialRulePriceDota($priceItem,$item['desc']);
			$lastPrice = $this->getRecoveryPrice($priceItem,$appId);
		}else{
			$lastPrice = $this->get578080Price($priceItem,$item['desc']);
		}

        //$step3Addition = Step3AdditionService::getInstance()->getStep3AdditionByAppIdAndItemType($priceItem['app_id'], $priceItem['item_type']);
        $step3AdditionConfigCacheKey = 'step3_addition_config_' . $priceItem['app_id'] . '_' . $priceItem['item_type'];
        $step3Addition = Cache::store('redis')->get($step3AdditionConfigCacheKey);

		if($lastPrice == false){
			$item['recovery'] = false;
			$item['price'] = 0;
			$item['item_second'] = $priceItem['second'];
			$item['fixPrice'] = false;
		}else{
			$item['recovery'] = true;

            if($priceItem['normal'] == 7 || $lastPrice['fixPrice'] == true){
                $item['fixPrice'] = true;
                $item['price'] = number_format($lastPrice['price'],2,'.','');
            }else{
                $item['price'] = number_format($lastPrice['price'] * (1 + $step3Addition/100),2,'.','');
                $item['fixPrice'] = false;
            }

			$item['item_second'] = $priceItem['second'];
		}
		return $item;
	}
	
	public function calculatePrice($userInventory,$appId = 730){
		$result = [];
		$nocanRecovery = [];
		$canRecorery = [];
		foreach ($userInventory['rgInventory'] as $item){
			$priceItem = SteamItem::getItemInfoByHashName($appId,$item['market_hash_name']);
			foreach ($userInventory['rgDescriptions'] as $rows => $res) {
				if ($item['classid'] == $res['classid'] && $item['instanceid'] == $res['instanceid']) {
                    $desc = $res;
                }
			}
			$item['desc'] = $desc;
			if(empty($priceItem) || !$item['tradable']){
				//我们数据库不存在这类商品的报价不收 
				$item['recovery'] = false;
				$item['price'] = 0;
				$item['originPrice'] = 0;
				$item['exterior'] = '';
				$item['rarity'] = 'b0c3d9';
				$item['item_second'] = -1;
				$nocanRecovery[] = $item;
				continue;
			}
			$item['originPrice'] = $priceItem['price'];
			$priceItem['price'] = number_format($priceItem['price'],2, '.' , '');
			
			if($priceItem['app_id'] == 730){
				$priceItem = $this->getSpecialRulePrice($priceItem,$desc);
				$lastPrice = $this->getRecoveryPrice($priceItem,$appId);
			}elseif($priceItem['app_id'] == 570){
				$priceItem = $this->getSpecialRulePriceDota($priceItem,$item['desc']);
				$lastPrice = $this->getRecoveryPrice($priceItem,$appId);
			}elseif($priceItem['app_id'] == 578080){
				$lastPrice = $this->get578080Price($priceItem,$desc);
			}
			
			if($lastPrice == false){
				$item['recovery'] = false;
				$item['price'] = 0;
				$item['item_second'] = -1;
				$item['fixPrice'] = false;
				$item['exterior'] = '';
				$item['rarity'] = '';
				$item['item_second'] = $priceItem['second'];
				$nocanRecovery[] = $item;
				continue;
			}
			$item['price'] = number_format($lastPrice['price'],2,'.','');


			$item['fixPrice'] = $lastPrice['fixPrice'];
			$item['recovery'] = true;
			$item['exterior'] = $priceItem['exterior'];
			$item['rarity'] = $priceItem['rarity'];
			$item['item_second'] = $priceItem['second'];

			$item['price'] = $item['price'] * $item['amount'];
			$canRecorery[] = $item;
		}
		$result = array_merge($canRecorery,$nocanRecovery);
		
		return $result;
	}
	
	private function get578080Price($priceItem, $desc){
		$priceItem['exterior'] = '';
		$priceItem['rarity'] = 'b0c3d9';
		$priceItem['fixPrice'] = false;

        $priceItem['price'] =number_format($priceItem['price'],2,'.','');

        if($priceItem['price'] <= 0){
            return false;
        }

		return $priceItem;
	}

	private function getRecoveryPrice($item,$appId=730){
		if(empty($item)){
			return false;
		}
		return $item;
		$price = $item['price'];
		
		if(number_format($price ,2,'.','') <= 0){
			return false;
		}

//		if($appId == 570 && $price<0.4){
//			return false;
//		}
//
//		if($appId == 570 && $price > 2800){
//			return false;
//		}

		if(!empty($item['fixPrice'])){
			return $item;
		}

        $priceRule = PriceRule::getRuleByPrice($price,$appId);

		if(empty($priceRule)){
			return false; //没有定价规则 
		}

		$steamPrice = $price;
		$buy_scale = $priceRule['buy_scale'];

//		if (!in_array($item['normal'], [6, 7])) {
//            if ($price <= 0.28 && $price > 0.21) {
//                $lastPrice = 0.04;
//                $item['price'] = $lastPrice;
//                return $item;
//            }
//            if ($price <= 0.21) {
//                $lastPrice = 0.03;
//                $item['price'] = $lastPrice;
//                return $item;
//            }
//        }

//		if($price <= 17.64){
//			$lastPrice = $steamPrice*$buy_scale*5/(6*1.18*100);
//		}elseif($price <= 1363.26){
//			$lastPrice = ($steamPrice*$buy_scale-2)/(1.18*100);
//		}elseif($price <= 10350){
//			$lastPrice = ($steamPrice*$buy_scale)/(1.002*1.18*100);
//		}else{
//			return false;
//		}

        if (in_array($item['item_type'], [2])) {
            $lastPrice = $steamPrice;
        } else {
            if (in_array($item['normal'], [7])) {
                $lastPrice = $steamPrice;
            }else{
                $lastPrice = $steamPrice * ($buy_scale / 100);
            }
        }

        $item['price'] = $lastPrice;
		if(number_format($item['price'] ,2,'.','') <= 0){
			return false;
		}
		return $item;
	}

    /**
     * Csgo物品特殊规则
     * @param $priceItem
     * @param $desc
     * @return mixed
     */
	private function getSpecialRulePrice($priceItem,$desc){
		$priceItem['exterior'] = '';
		$priceItem['rarity'] = 'b0c3d9';
		$priceItem['fixPrice'] = false;
		foreach ($desc['tags'] as $tag){
			if($tag['category'] == 'Type'){
//				switch ($tag['internal_name']){
//				//	case 'CSGO_Type_Pistol'://
////					case 'CSGO_Tool_WeaponCase_KeyTag':
////						$priceItem['price'] = 12;
////						$priceItem['fixPrice'] = true;
////						break;
//					case 'CSGO_Type_WeaponCase' ://武器箱
//					case 'CSGO_Tool_Sticker' ://印花
//					case 'CSGO_Type_Spray' ://涂鸦
//// 					case 'CSGO_Type_MusicKit' ://音乐盒
//					case 'CSGO_Type_Ticket' ://通行证
//					case 'CSGO_Tool_GiftTag' ://礼物
//					case 'CSGO_Type_Tool' ://工具
//					case 'CSGO_Tool_Name_TagTag' ://标签
//						$priceItem['price'] = number_format($priceItem['price'] * 0.5 , 2 , '.' , '');
//						break;
//				}
			}elseif ($tag['category'] == 'Rarity'){
				$priceItem['rarity'] = $tag['color'];
			}elseif  ($tag['category'] == 'Exterior'){
				$priceItem['exterior'] = $tag['localized_tag_name'];
			}elseif($tag['category'] == 'Quality' && $tag['internal_name']=='tournament'){
//				$priceItem['price'] = number_format($priceItem['price'] * 0.5 , 2 , '.' , '');
			}
		}
		return $priceItem;
	}
	
    /**
     * dota的物品特殊规则
     * @param $priceItem
     * @param $desc
     * @return bool
     */
	private function getSpecialRulePriceDota($priceItem,$desc){
		$priceItem['exterior'] = '';
		$priceItem['rarity'] = 'b0c3d9';
		$priceItem['fixPrice'] = false;
		if($priceItem['app_id'] != 570){
			return false;
		}
		$flag = false;
		foreach ($desc['tags'] as $tag){
			if($tag['category'] == 'Type'){
				$record = GameTagCategory::getByTypeAndValue($tag['category'],$tag['internal_name']);
				if(!empty($record)){
					$flag =  true;
					break;
				}
			}elseif ($tag['category'] == 'Rarity'){
				$priceItem['rarity'] = $tag['color'];
			}elseif  ($tag['category'] == 'Exterior'){
				$priceItem['exterior'] = $tag['name'];
			}
		}
		return $flag ? $priceItem : false;
		
	}
	
	
	
}

